package com.vincce.sport.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.entity.CompetitionSeason;
import com.vincce.sport.entity.Page;
import com.vincce.sport.mapper.CompetitionSeasonMapper;

@Controller
@RequestMapping("/competitionSeason")
public class CompetitionSeasonController extends BaseController{

	@Resource
	private CompetitionSeasonMapper competitionSeasonMapper;
	
	@RequestMapping("/list")
	public String list(Model model, Page<CompetitionSeason> page, CompetitionSeason competitionSeason){
        Map<String, Object> filter = new HashMap<String, Object>();
		page.setFilter(filter);
		Long total = competitionSeasonMapper.findCompetitionSeasonCount(page.getFilter());
		page.setTotal(total < 0 ? 1 : total);
		page.setResult(competitionSeasonMapper.findCompetitionSeasonList(page));
		model.addAttribute("page",page);
		return "/backstage/competitionSeasonList";
	}
	
	@RequestMapping("/addOrUpdate")
	@ResponseBody
	public String addOrUpdate(Model model,CompetitionSeason competitionSeason){
		Long userId = getCurrentUser().getId();
		Long count = 0L;
		if(CheckUtil.isEmpty(competitionSeason.getId())){
			competitionSeason.setStatus(2);
			competitionSeason.setCreateUser(userId);
			competitionSeason.setCreateTime(new Date());
			count = competitionSeasonMapper.insert(competitionSeason);
		}else{
			competitionSeason.setUpdateUser(userId);
			competitionSeason.setUpdateTime(new Date());
			count = competitionSeasonMapper.update(competitionSeason);
		}
		return JSON.toJSONString(count>0);
	}
	
	/**
	 * 检查赛季显示个数
	 * @return
	 */
	@RequestMapping("/checkShowSeason")
	@ResponseBody
	public String checkShowSeason(Model model, HttpServletRequest request, Integer status){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("status", status);
		List<CompetitionSeason> seasonList = competitionSeasonMapper.findCompetitionSeason(param);
		Integer count = CheckUtil.isEmpty(seasonList) ? 0 : seasonList.size();
		return JSON.toJSONString(count);
	}
	
	/**
	 * 显示赛季
	 * @return
	 */
	@RequestMapping("/showCompetitionSeason")
	@ResponseBody
	public String showCompetitionSeason(Model model, CompetitionSeason competitionSeason){
		Long userId = getCurrentUser().getId();
		Long count = 0L;
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("status", 1);
		List<CompetitionSeason> seasonList = competitionSeasonMapper.findCompetitionSeason(param);
		for(CompetitionSeason competitionSeason2 : seasonList){
			competitionSeason2.setStatus(2);
			competitionSeason2.setUpdateUser(userId);
			competitionSeason2.setUpdateTime(new Date());
			count = competitionSeasonMapper.update(competitionSeason2);
		}
		competitionSeason.setStatus(1);
		competitionSeason.setUpdateUser(userId);
		competitionSeason.setUpdateTime(new Date());
		count = competitionSeasonMapper.update(competitionSeason);
		return JSON.toJSONString(count);
	}
}
